package com.nullam.lorem.contactsync.activities;


import com.nullam.lorem.contactsync.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

@SuppressLint("NewApi")
public class ThirdActivity extends Activity {
	final static private String TAG = "ThirdActivity";

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.buttons_container);
		Button btn = null;
		for (int i = 0; i < 10; i++) {
			btn = new Button(this);
			String name = "Button : goBack " + String.valueOf(i);
			btn.setText(name);
			btn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					onBackButtonClick(v);
				}
			});
			linearLayout.addView(btn, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		}

		btn = new Button(this);
		String name = "Exit";
		btn.setText(name);
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onExitButtonClick(v);
			}
		});
		linearLayout.addView(btn, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));

	}

	public void onBackButtonClick(View view) {
		Intent it = new Intent(this, ContactDetailActivity.class);
		startActivity(it);
	}

	public void onExitButtonClick(View view) {
		Log.e(TAG, "Enter exit button clicked");
		AlertDialog.Builder alerBuilder = new AlertDialog.Builder(this);
		alerBuilder.setTitle("Confirmation");
		alerBuilder
				.setMessage("Do you want to exit?")
				.setCancelable(true)
				.setIcon(R.drawable.abc_ic_go)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Log.e(TAG, "You choose Yes");
								Intent intent = new Intent(Intent.ACTION_MAIN);
								intent.addCategory(Intent.CATEGORY_HOME);
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								startActivity(intent);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Log.e(TAG, "You choose No");
						dialog.cancel();

					}
				});
		// create alert dialog
		AlertDialog alertDialog = alerBuilder.create();

		// show it
		alertDialog.show();
	}
}
